home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 406_01 / disked25 / source / alloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-13  |  3.6 KB  |  172 lines

  1. /***
  2. *alloc.c - malloc/free front-end
  3. *
  4. *Copyright (c) 1993-1994, Gregg Jennings.  All wrongs reserved.
  5. *   P O Box 200, Falmouth, MA 02541-0200
  6. *
  7. *Purpose:
  8. *   Debugging support for DISKED.C.
  9. *
  10. *Notice:
  11. *   This progam may be freely used and distributed.  Any distrubution
  12. *   with modifications must retain the above copyright statement and
  13. *   modifications noted.
  14. *   No pulp-publication, in whole or in part, permitted without
  15. *   permission (magazines or books).
  16. *******************************************************************************/
  17.  
  18. /*
  19.    Version 2.1 13-Jan-1994    Borland stuff, heaptest was heapcheck
  20.    Version 2.0 28-Nov-1993
  21.  
  22.    Usage:
  23.  
  24.    these can be called explicitly (as DISKED does) or via macros:
  25.  
  26.    #define malloc(s)    alloc(1,s)
  27.    #define free(p)      freep(p)
  28.       etc.
  29.  
  30.    which will enable them to be used if needed for debugging
  31.  
  32. */
  33.  
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <malloc.h>
  37.  
  38. #include "alloc.h"
  39. #include "error.h"                        /* for global error messages */
  40.  
  41.  
  42. /*********** COMMON DATA ***********/
  43.  
  44. /* for use by debug.c -- statistical stuff only */
  45.  
  46. unsigned int blocks = 0;
  47. unsigned int nblocks = 0;
  48. unsigned int frees = 0;
  49. unsigned int nfrees = 0;
  50.  
  51. void *alloc(size_t num, size_t size)
  52. {
  53.    if (size == 0)                      /* NOTE: MSC WILL allocate */
  54.    {                                   /* a zero length block -- a pointer */
  55.       error.num = ALLOC_ZERO;          /* to nothing as paradoxical as */
  56.       return NULL;                     /* that sounds (is?) */
  57.    }
  58.    ++blocks;
  59.    return calloc(num,size);
  60. }
  61.  
  62. #ifndef __BORLANDC__
  63.  
  64. void _near *nalloc(size_t num, size_t size)
  65. {
  66.    if (size == 0)
  67.    {
  68.       error.num = ALLOC_ZERO;
  69.       return NULL;
  70.    }
  71.    ++nblocks;
  72.    return _ncalloc(num,size);
  73. }
  74.  
  75. #endif
  76.  
  77. /* realloc() */
  78.  
  79. void *newalloc(void *p, size_t size)
  80. {
  81.    if (p)
  82.       freep(p);
  83.    return alloc(1,size);
  84. }
  85.  
  86. void freep(void *addr)
  87. {
  88.    if (addr != NULL)
  89.    {
  90.       free(addr);
  91.       if (_heapset(254) != _HEAPOK)
  92.          error.num = HEAP_ERROR;
  93.       addr = NULL;
  94.       ++frees;
  95.    }
  96.    else
  97.       error.num = FREE_NULL;
  98. }
  99.  
  100. #ifndef __BORLANDC__
  101.  
  102. void nfreep(void _near *addr)
  103. {
  104.    if (addr != NULL)
  105.    {
  106.       _nfree(addr);
  107.       if (_nheapset(254) != _HEAPOK)
  108.          error.num = HEAP_ERROR;
  109.       addr = NULL;
  110.       ++nfrees;
  111.    }
  112.    else
  113.       error.num = FREE_NULL;
  114. }
  115.  
  116. #endif
  117.  
  118. /* heapcheck()
  119.  
  120.    putting calls to this throughout the program can help find were the
  121.    heap is getting corrupted:
  122.  
  123.       if (heaptest() < 1)
  124.          printf("%s%d",__FILE__,__LINE__);
  125. */
  126.  
  127. int heaptest(void)
  128. {
  129. int h;
  130.  
  131.    if ((h=_heapchk()) != _HEAPOK || (h=_nheapchk()) != _HEAPOK)
  132.       return h;
  133.    else
  134.       return 1;
  135. }
  136.  
  137. /* AS IN MALLOC.H:
  138.  
  139. constants for _heapchk/_heapset/_heapwalk routines
  140.  
  141. #define _HEAPEMPTY   (-1)
  142. #define _HEAPOK   (-2)           // _heapchk/_heapset only
  143. #define _HEAPBADBEGIN   (-3)
  144. #define _HEAPBADNODE (-4)
  145. #define _HEAPEND  (-5)           // _heapwalk only
  146. #define _HEAPBADPTR  (-6)
  147. #define _FREEENTRY   0
  148. #define _USEDENTRY   1
  149.  
  150. */
  151. static char *heap_msg[] = {
  152.    "",
  153.    "empty heap",
  154.    "heap is fine",
  155.    "bad start of heap",
  156.    "bad node in heap",
  157.    "end of heap",
  158.    "bad pointer to heap",
  159.    "bad heap",
  160. };
  161.  
  162. /* get heap status message */
  163.  
  164. char *heapstat(int status)
  165. {
  166.    status = -status;      /* make offset into message array */
  167.  
  168.    if (status < 1 || status > sizeof(heap_msg)/sizeof(char *))
  169.       status = sizeof(heap_msg)/sizeof(char *);
  170.    return heap_msg[status];
  171. }
  172.